package com.xsb.aidl_client

import android.app.Service
import android.content.ComponentName
import android.content.Intent
import android.content.ServiceConnection
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.IBinder
import android.widget.Button
import com.xsb.aidl.IDataInterface

class MainActivity : AppCompatActivity() {
    lateinit var  iDataInterface : IDataInterface
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        var conn = object : ServiceConnection {
            override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
                iDataInterface = IDataInterface.Stub.asInterface(service)
            }

            override fun onServiceDisconnected(name: ComponentName?) {
            }
        }
        var intent = Intent("MyService")
        intent.setPackage("com.xsb.aidl")
        findViewById<Button>(R.id.btn_start).setOnClickListener{bindService(intent,conn, Service.BIND_AUTO_CREATE).apply {
        }}

        findViewById<Button>(R.id.btn_end).setOnClickListener{
            unbindService(conn)
        }
        findViewById<Button>(R.id.btn_add).setOnClickListener{
            iDataInterface.addNum(2,4)
        }
    }
}
